// 引入相关模块
var http = require('http');
var url = require('url');
var path = require('path');
var readStaticFile = require('./modules/readStaticFile');

// 搭建 HTTP 服务器
var server = http.createServer(async function(req, res) {
  var urlObj = url.parse(req.url);
  // console.log(urlObj)
  var urlPathname = urlObj.pathname;
  // console.log(urlPathname)
  var filePathname = path.join(__dirname, "/public", urlPathname);
  // console.log(filePathname)
  // 读取静态文件
  let {mimeType, data} = await readStaticFile(filePathname);
  res.writeHead(200, {
    'content-type': `${mimeType};charset=utf-8`
  })
  res.end(data)
});

// 在 3000 端口监听请求
server.listen(3000, function() {
  console.log("服务器运行中.");
  console.log("正在监听 3000 端口:")
})